﻿using System;
using UniRx;
using UniRx.Triggers;
using UnityEngine;

namespace Studio
{
	// Token: 0x020005DC RID: 1500
	public class GripRotation : MonoBehaviour
	{
		// Token: 0x170004BA RID: 1210
		// (get) Token: 0x06002661 RID: 9825 RVA: 0x000E0CB0 File Offset: 0x000DEEB0
		private Transform transCamera
		{
			get
			{
				if (this.m_TransCamera == null)
				{
					this.m_TransCamera = this.objCameraRig.transform;
				}
				return this.m_TransCamera;
			}
		}

		// Token: 0x06002662 RID: 9826 RVA: 0x000E0CDC File Offset: 0x000DEEDC
		private void StartRotation()
		{
			this.transTrack = this.trackedObject.transform;
			this.posOld = this.transTrack.position;
			this.basePos = this.transCamera.position;
			this.baseRot = this.transCamera.rotation;
			this.baseDir = this.posOld - this.basePos;
			this.baseDir.y = 0f;
			this.baseDir.Normalize();
		}

		// Token: 0x06002663 RID: 9827 RVA: 0x000E0D60 File Offset: 0x000DEF60
		private void Rotation()
		{
			if (this.transTrack == null)
			{
				return;
			}
			Vector3 vector = this.transTrack.position - this.basePos;
			vector.y = 0f;
			Vector3 vector2 = this.transTrack.position - this.posOld;
			vector2.y = 0f;
			if (vector2.magnitude >= 0.001f)
			{
				this.transCamera.rotation = this.baseRot * Quaternion.FromToRotation(vector.normalized, this.baseDir);
			}
			this.posOld = this.transTrack.position;
		}

		// Token: 0x06002664 RID: 9828 RVA: 0x000E0E10 File Offset: 0x000DF010
		private void EndRotation()
		{
			this.transTrack = null;
		}

		// Token: 0x06002665 RID: 9829 RVA: 0x000E0E1C File Offset: 0x000DF01C
		private void Start()
		{
			SteamVR_Controller.Device device = SteamVR_Controller.Input((int)this.trackedObject.index);
			(from _ in this.UpdateAsObservable()
			where this.enabled
			where device.GetPressDown(4UL)
			select _).Subscribe(delegate(Unit _)
			{
				this.StartRotation();
			});
			(from _ in this.UpdateAsObservable()
			where this.enabled
			where device.GetPress(4UL)
			select _).Subscribe(delegate(Unit _)
			{
				this.Rotation();
			});
			(from _ in this.UpdateAsObservable()
			where this.enabled
			where device.GetPressUp(4UL)
			select _).Subscribe(delegate(Unit _)
			{
				this.EndRotation();
			});
		}

		// Token: 0x04002671 RID: 9841
		[SerializeField]
		private GameObject objCameraRig;

		// Token: 0x04002672 RID: 9842
		private Transform m_TransCamera;

		// Token: 0x04002673 RID: 9843
		[SerializeField]
		private SteamVR_TrackedObject trackedObject;

		// Token: 0x04002674 RID: 9844
		private Vector3 posOld = Vector3.zero;

		// Token: 0x04002675 RID: 9845
		private Vector3 baseDir = Vector3.zero;

		// Token: 0x04002676 RID: 9846
		private Transform transTrack;

		// Token: 0x04002677 RID: 9847
		private Vector3 basePos = Vector3.zero;

		// Token: 0x04002678 RID: 9848
		private Quaternion baseRot = Quaternion.identity;
	}
}
